package cn.xshi.oauth.web;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import cn.xshi.oauth.model.OauthKeyInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.base.BaseSearch;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.StringUtil;
import cn.xshi.oauth.service.OauthKeyInfoService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
/**
 * @Desc 授权中心密钥管理
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/oauthKeyInfo")
@Api(value = "授权中心密钥管理API",tags = "授权中心密钥管理API",description = "授权中心密钥管理API")
public class OauthKeyInfoController extends BaseAction {

	@Resource
	private OauthKeyInfoService oauthKeyInfoService;

	/**
	* 查询秘钥列表并分页
	* @param baseSearch 
	*/
	@ApiOperation(value="查询秘钥列表并分页", notes="查询秘钥列表并分页")
	@PostMapping(value="/list")
	@Auth(value = "/oauthKeyInfo/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BasePage<List<OauthKeyInfo>> getOauthKeyInfoListByCondition(@RequestBody BaseSearch baseSearch){
		Map<String, Object> condition = baseSearch.convert();
		commonHPager(baseSearch);
		List<OauthKeyInfo> oauthKeyInfoList = oauthKeyInfoService.getOauthKeyInfoListByCondition(condition);
		for(OauthKeyInfo oauthKeyInfo:oauthKeyInfoList){
			if(!StringUtil.isEmpty(oauthKeyInfo.getCreateId())){
				OauthAccountEntity createBy = getAccount(oauthKeyInfo.getCreateId());
				if(null != createBy){
					oauthKeyInfo.setCreateBy(createBy.getName());
				}
			}
			if(!StringUtil.isEmpty(oauthKeyInfo.getUpdateId())){
				OauthAccountEntity modifiedBy = getAccount(oauthKeyInfo.getUpdateId());
				if(null != modifiedBy){
					oauthKeyInfo.setModifiedBy(modifiedBy.getName());
				}
			}
		}
		PageInfo<OauthKeyInfo> page = new PageInfo<OauthKeyInfo>(oauthKeyInfoList);
		return outPageBootStr(page,baseSearch);
	}

	/**
	 * 查询全部秘钥列表
	 * @return
	 */
	@ApiOperation(value="查询全部秘钥列表", notes="查询全部秘钥列表")
	@GetMapping(value="/listAll")
	@Auth(value = "/oauthKeyInfo/listAll",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<List<OauthKeyInfo>> listAll(){
		Map<String, Object> condition = new HashMap<>();
		List<OauthKeyInfo> oauthKeyInfoList = oauthKeyInfoService.getOauthKeyInfoListByCondition(condition);
		return outDataStr(oauthKeyInfoList);
	}

	/**
	* 查询单个秘钥
	* @param id
	*/
	@ApiOperation(value="查询单个秘钥", notes="查询单个秘钥")
	@GetMapping(value="/get/{id}")
	@Auth(value = "/oauthKeyInfo/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<OauthKeyInfo> getOauthKeyInfoById(@PathVariable("id")String id){
		OauthKeyInfo oauthKeyInfo = oauthKeyInfoService.getOauthKeyInfoById(id);
		if(!StringUtil.isEmpty(oauthKeyInfo.getCreateId())){
			OauthAccountEntity createBy = getAccount(oauthKeyInfo.getCreateId());
			if(null != createBy){
				oauthKeyInfo.setCreateBy(createBy.getName());
			}
		}
		if(!StringUtil.isEmpty(oauthKeyInfo.getUpdateId())){
			OauthAccountEntity modifiedBy = getAccount(oauthKeyInfo.getUpdateId());
			if(null != modifiedBy){
				oauthKeyInfo.setModifiedBy(modifiedBy.getName());
			}
		}
		return outDataStr(oauthKeyInfo);
	}

	/**
	* 添加
	* @param oauthKeyInfo 
	*/
	@PostMapping(value="/add")
	@ApiOperation(value="创建单个秘钥", notes="创建单个秘钥")
	public BaseResult addOauthKeyInfo(@RequestBody OauthKeyInfo oauthKeyInfo){
		int i = 0;
		if(null != oauthKeyInfo){
			i=oauthKeyInfoService.addOauthKeyInfo(oauthKeyInfo);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 修改
	* @param oauthKeyInfo 
	*/
	@PutMapping(value="/update")
	@ApiOperation(value="编辑单个秘钥", notes="编辑单个秘钥")
	public BaseResult updateOauthKeyInfo(@RequestBody OauthKeyInfo oauthKeyInfo){
		int i = 0;
		if(null != oauthKeyInfo){
			i=oauthKeyInfoService.updateOauthKeyInfo(oauthKeyInfo);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 删除
	* @param id
	*/
	@DeleteMapping(value="/delete")
	@ApiOperation(value="删除秘钥", notes="删除秘钥")
	public BaseResult delOauthKeyInfo(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i=oauthKeyInfoService.delOauthKeyInfo(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
}
